layui.use(['form', 'table','layuimini','element', 'tableSelect', 'laydate'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layuimini = layui.layuimini,
        laydate = layui.laydate,
        tableSelect = layui.tableSelect;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    var BoPersonalDiary = {
        id: 'BoPersonalDiaryTable'
    };

    var column = [
        [
            {type: 'numbers'},
            {field: 'title', title: '标题', align: 'center'},
            {field: 'type', title: '日记类型', align: 'center',
                templet: function (object) {
                    if(object.type == '0'){
                        return '<span class="layui-badge layui-bg-green">个人日记</span>';
                    }else{
                        return '<span class="layui-badge">隐私日记</span>';
                    }
                }
            },
            {field: '', title: '日志内容', align: 'center',
                templet: function (object) {
                    return '<span style="color: #FF5722;">内容不显示</span>';
                }
            },
            {field: 'remark', title: '备注', align: 'center',
                templet: function (object) {
                    if(object.remark != "" && object.remark != null){
                        return object.remark;
                    }else{
                        return "-";
                    }
                }
            },
            {field: 'sendTime', title: '日志时间', align: 'center'},
            {field: 'author', title: '发布人', align: 'center',
                templet: function (object) {
                    if(object.author != "" && object.author != null){
                        return object.author;
                    }else{
                        return "-";
                    }
                }
            },
            {field: 'updateDate', title: '更新时间', minWidth: 120, align: 'center'},
            {templet: '#stateBtn', width: 140, align: 'center', title: '操作'}
        ]
    ];

    table.render({
        elem: '#' + BoPersonalDiary.id,
        url: '/web/boPersonalDiary/list',
        toolbar: '#toolbarBtn',
        defaultToolbar: ['filter', 'exports', 'print'],
        cols: column,
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true
    });

    //日志时间范围
    laydate.render({
        elem: '#search_sendTime'
        ,range: true
    });

    // 监听搜索操作
    $("#data-search-btn").on('click', function () {

        //获取搜索条件
        var author = $("#search_author").val();
        var sendTime = $("#search_sendTime").val();
        var type = $("#search_type").val();

        //封装
        var dataObject = {};
        dataObject['author'] = author;
        dataObject['sendTime'] = sendTime;
        dataObject['type'] = type;
        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(BoPersonalDiary.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    // 监听重置操作
    $("#data-reset-btn").on('click', function () {

        //初始化
        $("#search_author").val("");
        $("#search_sendTime").val("");
        $("#search_type").val("-1");

        form.render();

        //封装
        var dataObject = {};
        dataObject['author'] = "";
        dataObject['sendTime'] = "";
        dataObject['type'] = "-1";
        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(BoPersonalDiary.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    // 监听添加操作
    $(".layuimini-container").on("click", "#add-btn", function () {
        var content = layuimini.getHrefContent('/web/boPersonalDiary/addOrEdit');
        var index = layer.open({
            title: '写日记',
            type: 1,
            shade: 0.2,
            maxmin:true,
            shadeClose: false,
            area: ['80%','90%'], //宽高
            content: content,
            end: function(){
                //刷新表格
                table.reload(BoPersonalDiary.id);
            }
        });

        return true;
    });

    /**
     * 点击编辑按钮
     * @param id
     */
    BoPersonalDiary.edit = function(id){
        var content = layuimini.getHrefContent('/web/boPersonalDiary/addOrEdit?id=' + id);
        var index = layer.open({
            title: '编辑日记',
            type: 1,
            shade: 0.2,
            maxmin:true,
            shadeClose: false,
            area: ['80%','90%'], //宽高
            content: content,
            end: function(){
                //刷新表格
                table.reload(BoPersonalDiary.id);
            }
        });

        return false;
    };

    /**
     * 点击删除按钮
     * @param id
     */
    BoPersonalDiary.delete = function(id){
        layer.open({
            title: '删除日记'
            ,content: '是否确定删除该日记？'
            ,icon: 3
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                layer.close(index);
                var indexLoad = layer.load(2);
                var url = '/web/boPersonalDiary/delete';
                $.post(url, {id: id}, function(data){
                    layer.close(indexLoad);
                    if(data.state == "200"){
                        layer.msg(data.message, {icon: 1});
                        table.reload(BoPersonalDiary.id);
                    }else{
                        layer.msg(data.message, {icon: 2});
                    }
                })
            }
        });

        return false;
    };


    //监听表格复选框选择
    // table.on('checkbox(currentTableFilter)', function (obj) {
    //     console.log(obj)
    // });

    //监听工具条
    table.on('tool(' + BoPersonalDiary.id + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'delete') {
            BoPersonalDiary.delete(data.id);
        } else if (layEvent === 'edit') {
            BoPersonalDiary.edit(data.id)
        }
    });

});